package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/14.<br><br>
 *
 * 找出数组中的幸运数
 */
public class Topic1394 {

    public static void main(String[] args) {
        Topic1394 instance = new Topic1394();

        int[] array = new int[] {2,2,3,4};
        System.out.println(instance.findLucky(array));

        array = new int[] {1,2,2,3,3,3};
        System.out.println(instance.findLucky(array));

        array = new int[] {2,2,2,3,3};
        System.out.println(instance.findLucky(array));

        array = new int[] {5};
        System.out.println(instance.findLucky(array));

        array = new int[] {7,7,7,7,7,7,7};
        System.out.println(instance.findLucky(array));
    }

    public int findLucky(int[] arr) {
        int[] counter = new int[arr.length];
        for(int value : arr) {
            if(value <= arr.length) {
                counter[value - 1]++;
            }
        }

        for(int i = counter.length - 1; i >= 0; i--) {
            if(counter[i] == i + 1) {
                return i + 1;
            }
        }

        return -1;
    }
}
